#coding=utf-8
#Boa:Dialog:DlgEditPro

import wx
import wx.grid
from product import *
from config import *
import DlgCategory,DlgKeyword
import kit

def create(parent):
    return DlgEditPro(parent)

[wxID_DLGEDITPRO, wxID_DLGEDITPROBTNCANCEL, wxID_DLGEDITPROBTNSAVE, 
 wxID_DLGEDITPROBTNSEARCHCATEGORY, wxID_DLGEDITPROBTNSEARCHKEYWORD, 
 wxID_DLGEDITPROBTNSTOCK, wxID_DLGEDITPROBTNSWITCH, 
 wxID_DLGEDITPROCHECKBOX_OVERSTOCK, wxID_DLGEDITPROCHECKBOX_PROMOT, 
 wxID_DLGEDITPROCHECKONUSE, wxID_DLGEDITPROCHK_NOTCOUNTABLE, 
 wxID_DLGEDITPROGRIDUNIT, wxID_DLGEDITPROSTATICBOX_UNIT, 
 wxID_DLGEDITPROSTATICTEXT1, wxID_DLGEDITPROSTATICTEXT10, 
 wxID_DLGEDITPROSTATICTEXT11, wxID_DLGEDITPROSTATICTEXT12, 
 wxID_DLGEDITPROSTATICTEXT13, wxID_DLGEDITPROSTATICTEXT14, 
 wxID_DLGEDITPROSTATICTEXT2, wxID_DLGEDITPROSTATICTEXT3, 
 wxID_DLGEDITPROSTATICTEXT4, wxID_DLGEDITPROSTATICTEXT5, 
 wxID_DLGEDITPROSTATICTEXT6, wxID_DLGEDITPROSTATICTEXT7, 
 wxID_DLGEDITPROSTATICTEXT8, wxID_DLGEDITPROSTATICTEXT9, 
 wxID_DLGEDITPROTBRAND, wxID_DLGEDITPROTCAREA, wxID_DLGEDITPROTCBRAND, 
 wxID_DLGEDITPROTCBULKPROFIT, wxID_DLGEDITPROTCCAPACITY, wxID_DLGEDITPROTCID, 
 wxID_DLGEDITPROTCIMAGE, wxID_DLGEDITPROTCKEYWORD, wxID_DLGEDITPROTCLASTCOST, 
 wxID_DLGEDITPROTCLENGTH, wxID_DLGEDITPROTCLOWESTQTY, 
 wxID_DLGEDITPROTCLXPROFIT, wxID_DLGEDITPROTCMEMO, wxID_DLGEDITPROTCMINPROFIT, 
 wxID_DLGEDITPROTCNAME, wxID_DLGEDITPROTCPACKAGE, wxID_DLGEDITPROTCPOSITION, 
 wxID_DLGEDITPROTCSIMCODE, wxID_DLGEDITPROTCSPEC, wxID_DLGEDITPROTCTYPE, 
 wxID_DLGEDITPROTC_FREIGHT_CHARGES, wxID_DLGEDITPROTC_WEIGHT, 
 wxID_DLGEDITPROTID, wxID_DLGEDITPROTKEYWORD, wxID_DLGEDITPROTMEMO, 
 wxID_DLGEDITPROTNAME, wxID_DLGEDITPROTPOSITION, wxID_DLGEDITPROTSIMCODE, 
 wxID_DLGEDITPROTSPEC, wxID_DLGEDITPROTTYPE, 
] = [wx.NewId() for _init_ctrls in range(57)]

class DlgEditPro(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_DLGEDITPRO, name=u'DlgEditPro',
              parent=prnt, pos=wx.Point(553, 348), size=wx.Size(954, 626),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'\u5546\u54c1\u4fe1\u606f')
        self.SetClientSize(wx.Size(938, 588))
        self.SetToolTipString(u'')
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.Center(wx.BOTH)

        self.TCName = wx.TextCtrl(id=wxID_DLGEDITPROTCNAME, name=u'TCName',
              parent=self, pos=wx.Point(160, 32), size=wx.Size(392, 22),
              style=0, value=u'')
        self.TCName.Bind(wx.EVT_TEXT, self.OnTCNameText,
              id=wxID_DLGEDITPROTCNAME)

        self.TName = wx.StaticText(id=wxID_DLGEDITPROTNAME,
              label=u'\u540d\u79f0\uff1a', name=u'TName', parent=self,
              pos=wx.Point(76, 31), size=wx.Size(56, 17), style=0)
        self.TName.SetAutoLayout(False)

        self.TID = wx.StaticText(id=wxID_DLGEDITPROTID,
              label=u'\u7f16\u53f7\uff1a', name=u'TID', parent=self,
              pos=wx.Point(592, 32), size=wx.Size(52, 17), style=0)
        self.TID.SetAutoLayout(False)

        self.TCID = wx.TextCtrl(id=wxID_DLGEDITPROTCID, name=u'TCID',
              parent=self, pos=wx.Point(656, 32), size=wx.Size(112, 22),
              style=0, value=u'')
        self.TCID.SetEditable(False)
        self.TCID.Enable(False)

        self.TSimCode = wx.StaticText(id=wxID_DLGEDITPROTSIMCODE,
              label=u'\u7b80\u7801\uff1a', name=u'TSimCode', parent=self,
              pos=wx.Point(592, 97), size=wx.Size(56, 15), style=0)

        self.TCSimCode = wx.TextCtrl(id=wxID_DLGEDITPROTCSIMCODE,
              name=u'TCSimCode', parent=self, pos=wx.Point(656, 90),
              size=wx.Size(112, 22), style=0, value=u'')

        self.CheckOnUse = wx.CheckBox(id=wxID_DLGEDITPROCHECKONUSE,
              label=u'\u542f\u7528', name=u'CheckOnUse', parent=self,
              pos=wx.Point(816, 32), size=wx.Size(120, 24), style=0)
        self.CheckOnUse.SetValue(True)
        self.CheckOnUse.Enable(True)

        self.TBrand = wx.StaticText(id=wxID_DLGEDITPROTBRAND,
              label=u'\u54c1\u724c\uff1a', name=u'TBrand', parent=self,
              pos=wx.Point(592, 64), size=wx.Size(51, 16), style=0)
        self.TBrand.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))

        self.TCBrand = wx.TextCtrl(id=wxID_DLGEDITPROTCBRAND, name=u'TCBrand',
              parent=self, pos=wx.Point(656, 61), size=wx.Size(112, 22),
              style=0, value=u'')

        self.TCSpec = wx.TextCtrl(id=wxID_DLGEDITPROTCSPEC, name=u'TCSpec',
              parent=self, pos=wx.Point(160, 69), size=wx.Size(392, 22),
              style=0, value=u'')

        self.TSpec = wx.StaticText(id=wxID_DLGEDITPROTSPEC,
              label=u'\u89c4\u683c\uff1a', name=u'TSpec', parent=self,
              pos=wx.Point(76, 72), size=wx.Size(64, 16), style=0)

        self.TCType = wx.TextCtrl(id=wxID_DLGEDITPROTCTYPE, name=u'TCType',
              parent=self, pos=wx.Point(160, 106), size=wx.Size(96, 22),
              style=0, value=u'')
        self.TCType.SetEditable(False)

        self.TType = wx.StaticText(id=wxID_DLGEDITPROTTYPE,
              label=u'\u7c7b\u578b\uff1a', name=u'TType', parent=self,
              pos=wx.Point(76, 113), size=wx.Size(64, 24), style=0)

        self.btnSearchCategory = wx.Button(id=wxID_DLGEDITPROBTNSEARCHCATEGORY,
              label=u'+', name=u'btnSearchCategory', parent=self,
              pos=wx.Point(264, 105), size=wx.Size(25, 25), style=0)
        self.btnSearchCategory.Bind(wx.EVT_BUTTON,
              self.OnBtnSearchCategoryButton,
              id=wxID_DLGEDITPROBTNSEARCHCATEGORY)

        self.TKeyword = wx.StaticText(id=wxID_DLGEDITPROTKEYWORD,
              label=u'\u5173\u952e\u5b57\uff1a', name=u'TKeyword', parent=self,
              pos=wx.Point(76, 148), size=wx.Size(68, 16), style=0)

        self.tcKeyword = wx.TextCtrl(id=wxID_DLGEDITPROTCKEYWORD,
              name=u'tcKeyword', parent=self, pos=wx.Point(160, 143),
              size=wx.Size(192, 26), style=0, value=u'')
        self.tcKeyword.SetEditable(False)

        self.btnSearchKeyword = wx.Button(id=wxID_DLGEDITPROBTNSEARCHKEYWORD,
              label=u'+', name=u'btnSearchKeyword', parent=self,
              pos=wx.Point(360, 144), size=wx.Size(25, 25), style=0)
        self.btnSearchKeyword.Bind(wx.EVT_BUTTON, self.OnBtnSearchKeywordButton,
              id=wxID_DLGEDITPROBTNSEARCHKEYWORD)

        self.gridUnit = wx.grid.Grid(id=wxID_DLGEDITPROGRIDUNIT,
              name=u'gridUnit', parent=self, pos=wx.Point(152, 312),
              size=wx.Size(520, 120), style=0)
        self.gridUnit.SetDefaultColSize(70)
        self.gridUnit.SetDefaultRowSize(25)
        self.gridUnit.SetRowLabelSize(15)
        self.gridUnit.SetColLabelSize(25)
        self.gridUnit.Bind(wx.grid.EVT_GRID_CELL_CHANGE,
              self.OnGridUnitGridCellChange)

        self.staticBox_unit = wx.StaticBox(id=wxID_DLGEDITPROSTATICBOX_UNIT,
              label=u'\u5355\u4f4d', name=u'staticBox_unit', parent=self,
              pos=wx.Point(80, 296), size=wx.Size(728, 144), style=0)

        self.btnSave = wx.Button(id=wxID_DLGEDITPROBTNSAVE,
              label=u'\u4fdd\u5b58', name=u'btnSave', parent=self,
              pos=wx.Point(624, 536), size=wx.Size(75, 26), style=0)
        self.btnSave.Bind(wx.EVT_BUTTON, self.OnBtnSaveButton,
              id=wxID_DLGEDITPROBTNSAVE)

        self.btnCancel = wx.Button(id=wxID_DLGEDITPROBTNCANCEL,
              label=u'\u53d6\u6d88', name=u'btnCancel', parent=self,
              pos=wx.Point(728, 536), size=wx.Size(75, 26), style=0)
        self.btnCancel.Bind(wx.EVT_BUTTON, self.OnBtnCancelButton,
              id=wxID_DLGEDITPROBTNCANCEL)

        self.TCMemo = wx.TextCtrl(id=wxID_DLGEDITPROTCMEMO, name=u'TCMemo',
              parent=self, pos=wx.Point(152, 448), size=wx.Size(648, 72),
              style=wx.HSCROLL | wx.TE_MULTILINE, value=u'')

        self.TMemo = wx.StaticText(id=wxID_DLGEDITPROTMEMO,
              label=u'\u5907\u6ce8\uff1a', name=u'TMemo', parent=self,
              pos=wx.Point(88, 456), size=wx.Size(51, 16), style=0)

        self.btnSwitch = wx.Button(id=wxID_DLGEDITPROBTNSWITCH,
              label=u'\u5207\u6362\u5230\u7b80\u6d01\u6a21\u5f0f',
              name=u'btnSwitch', parent=self, pos=wx.Point(88, 536),
              size=wx.Size(144, 26), style=0)
        self.btnSwitch.Bind(wx.EVT_BUTTON, self.OnBtnSwitchButton,
              id=wxID_DLGEDITPROBTNSWITCH)

        self.TPosition = wx.StaticText(id=wxID_DLGEDITPROTPOSITION,
              label=u'\u4ed3\u4f4d\uff1a', name=u'TPosition', parent=self,
              pos=wx.Point(864, 348), size=wx.Size(51, 16), style=0)
        self.TPosition.Show(False)

        self.TCPosition = wx.TextCtrl(id=wxID_DLGEDITPROTCPOSITION,
              name=u'TCPosition', parent=self, pos=wx.Point(856, 383),
              size=wx.Size(112, 26), style=0, value=u'')
        self.TCPosition.Show(False)

        self.chk_notcountable = wx.CheckBox(id=wxID_DLGEDITPROCHK_NOTCOUNTABLE,
              label=u'\u4e0d\u8ba1\u6570', name=u'chk_notcountable',
              parent=self, pos=wx.Point(816, 64), size=wx.Size(120, 24),
              style=0)
        self.chk_notcountable.SetValue(True)

        self.staticText1 = wx.StaticText(id=wxID_DLGEDITPROSTATICTEXT1,
              label=u'\u5e93\u5b58\u9884\u8b66\uff1a', name='staticText1',
              parent=self, pos=wx.Point(728, 185), size=wx.Size(85, 16),
              style=0)

        self.TCLowestQty = wx.TextCtrl(id=wxID_DLGEDITPROTCLOWESTQTY,
              name=u'TCLowestQty', parent=self, pos=wx.Point(816, 180),
              size=wx.Size(56, 26), style=0, value=u'0')

        self.staticText4 = wx.StaticText(id=wxID_DLGEDITPROSTATICTEXT4,
              label=u'\u91cd\u91cf\uff1a', name='staticText4', parent=self,
              pos=wx.Point(592, 264), size=wx.Size(51, 24), style=0)

        self.tc_weight = wx.TextCtrl(id=wxID_DLGEDITPROTC_WEIGHT,
              name=u'tc_weight', parent=self, pos=wx.Point(656, 257),
              size=wx.Size(56, 26), style=0, value=u'0')

        self.staticText5 = wx.StaticText(id=wxID_DLGEDITPROSTATICTEXT5,
              label=u'kg', name='staticText5', parent=self, pos=wx.Point(720,
              266), size=wx.Size(24, 24), style=0)

        self.tc_freight_charges = wx.TextCtrl(id=wxID_DLGEDITPROTC_FREIGHT_CHARGES,
              name=u'tc_freight_charges', parent=self, pos=wx.Point(464, 256),
              size=wx.Size(96, 26), style=0, value=u'0')

        self.staticText6 = wx.StaticText(id=wxID_DLGEDITPROSTATICTEXT6,
              label=u'\u9ed8\u8ba4\u8fd0\u8d39\uff1a', name='staticText6',
              parent=self, pos=wx.Point(360, 258), size=wx.Size(88, 22),
              style=0)

        self.staticText2 = wx.StaticText(id=wxID_DLGEDITPROSTATICTEXT2,
              label=u'\u5e95\u4ef7   = \u6700\u8fd1\u5165\u5e93\u4ef7 + ',
              name='staticText2', parent=self, pos=wx.Point(76, 185),
              size=wx.Size(191, 16), style=0)

        self.staticText3 = wx.StaticText(id=wxID_DLGEDITPROSTATICTEXT3,
              label=u'\u8001\u4e61\u4ef7 = \u6700\u8fd1\u5165\u5e93\u4ef7 + ',
              name='staticText3', parent=self, pos=wx.Point(76, 222),
              size=wx.Size(190, 16), style=0)

        self.staticText7 = wx.StaticText(id=wxID_DLGEDITPROSTATICTEXT7,
              label=u'\u6279\u91cf\u4ef7 = \u6700\u8fd1\u5165\u5e93\u4ef7 + ',
              name='staticText7', parent=self, pos=wx.Point(76, 261),
              size=wx.Size(190, 16), style=0)

        self.tcMinProfit = wx.TextCtrl(id=wxID_DLGEDITPROTCMINPROFIT,
              name=u'tcMinProfit', parent=self, pos=wx.Point(272, 176),
              size=wx.Size(80, 30), style=0, value=u'0')
        self.tcMinProfit.Bind(wx.EVT_TEXT, self.OnTcMinProfitText,
              id=wxID_DLGEDITPROTCMINPROFIT)

        self.tcLxProfit = wx.TextCtrl(id=wxID_DLGEDITPROTCLXPROFIT,
              name=u'tcLxProfit', parent=self, pos=wx.Point(272, 216),
              size=wx.Size(80, 30), style=0, value=u'0')
        self.tcLxProfit.Bind(wx.EVT_TEXT, self.OnTcLxProfitText,
              id=wxID_DLGEDITPROTCLXPROFIT)

        self.tcBulkProfit = wx.TextCtrl(id=wxID_DLGEDITPROTCBULKPROFIT,
              name=u'tcBulkProfit', parent=self, pos=wx.Point(272, 256),
              size=wx.Size(80, 30), style=0, value=u'0')
        self.tcBulkProfit.Bind(wx.EVT_TEXT, self.OnTcBulkProfitText,
              id=wxID_DLGEDITPROTCBULKPROFIT)

        self.staticText8 = wx.StaticText(id=wxID_DLGEDITPROSTATICTEXT8,
              label=u'30\u5929\u9500\u91cf\uff08\u91c7\u8d2d\u95f4\u9694\uff09\n\u6216\u5355\u7b14\u6700\u5927\u9500\u91cf',
              name='staticText8', parent=self, pos=wx.Point(816, 208),
              size=wx.Size(122, 30), style=0)
        self.staticText8.SetFont(wx.Font(9, wx.SWISS, wx.NORMAL, wx.NORMAL,
              False, u''))

        self.checkBox_overstock = wx.CheckBox(id=wxID_DLGEDITPROCHECKBOX_OVERSTOCK,
              label=u'\u79ef\u538b/\u6ede\u9500', name=u'checkBox_overstock',
              parent=self, pos=wx.Point(816, 96), size=wx.Size(105, 16),
              style=0)
        self.checkBox_overstock.SetValue(True)

        self.checkBox_promot = wx.CheckBox(id=wxID_DLGEDITPROCHECKBOX_PROMOT,
              label=u'\u4fc3\u9500\u5546\u54c1', name=u'checkBox_promot',
              parent=self, pos=wx.Point(816, 120), size=wx.Size(105, 16),
              style=0)
        self.checkBox_promot.SetValue(False)

        self.staticText9 = wx.StaticText(id=wxID_DLGEDITPROSTATICTEXT9,
              label=u'\u56fe\u7247\u524d\u7f00\uff1a', name='staticText9',
              parent=self, pos=wx.Point(360, 184), size=wx.Size(85, 16),
              style=0)

        self.staticText10 = wx.StaticText(id=wxID_DLGEDITPROSTATICTEXT10,
              label=u'\u6700\u8fd1\u5165\u5e93\u4ef7\uff1a',
              name='staticText10', parent=self, pos=wx.Point(360, 224),
              size=wx.Size(102, 16), style=0)

        self.staticText11 = wx.StaticText(id=wxID_DLGEDITPROSTATICTEXT11,
              label=u'\u4ed3\u4f4d\u5bb9\u91cf\uff1a', name='staticText11',
              parent=self, pos=wx.Point(848, 424), size=wx.Size(85, 16),
              style=0)
        self.staticText11.Show(False)

        self.tcImage = wx.TextCtrl(id=wxID_DLGEDITPROTCIMAGE, name=u'tcImage',
              parent=self, pos=wx.Point(464, 176), size=wx.Size(96, 26),
              style=0, value=u'')

        self.tcLastCost = wx.TextCtrl(id=wxID_DLGEDITPROTCLASTCOST,
              name=u'tcLastCost', parent=self, pos=wx.Point(464, 216),
              size=wx.Size(96, 26), style=0, value=u'0')

        self.tcCapacity = wx.TextCtrl(id=wxID_DLGEDITPROTCCAPACITY,
              name=u'tcCapacity', parent=self, pos=wx.Point(856, 456),
              size=wx.Size(112, 26), style=0, value=u'0')
        self.tcCapacity.Show(False)

        self.btnStock = wx.Button(id=wxID_DLGEDITPROBTNSTOCK,
              label=u'\u7f16\u8f91\u5e93\u5b58', name=u'btnStock', parent=self,
              pos=wx.Point(496, 536), size=wx.Size(93, 26), style=0)
        self.btnStock.Bind(wx.EVT_BUTTON, self.OnBtnStockButton,
              id=wxID_DLGEDITPROBTNSTOCK)

        self.staticText12 = wx.StaticText(id=wxID_DLGEDITPROSTATICTEXT12,
              label=u'\u957f\u5ea6\uff1a', name='staticText12', parent=self,
              pos=wx.Point(592, 224), size=wx.Size(51, 16), style=0)

        self.tcLength = wx.TextCtrl(id=wxID_DLGEDITPROTCLENGTH,
              name=u'tcLength', parent=self, pos=wx.Point(656, 216),
              size=wx.Size(56, 26), style=0, value=u'0')

        self.staticText13 = wx.StaticText(id=wxID_DLGEDITPROSTATICTEXT13,
              label=u'\u5e73\u65b9\uff1a', name='staticText13', parent=self,
              pos=wx.Point(592, 184), size=wx.Size(51, 16), style=0)

        self.tcArea = wx.TextCtrl(id=wxID_DLGEDITPROTCAREA, name=u'tcArea',
              parent=self, pos=wx.Point(656, 176), size=wx.Size(56, 26),
              style=0, value=u'0')

        self.staticText14 = wx.StaticText(id=wxID_DLGEDITPROSTATICTEXT14,
              label=u'\u5305\u88c5\uff1a', name='staticText14', parent=self,
              pos=wx.Point(760, 264), size=wx.Size(51, 16), style=0)

        self.tcPackage = wx.TextCtrl(id=wxID_DLGEDITPROTCPACKAGE,
              name=u'tcPackage', parent=self, pos=wx.Point(816, 256),
              size=wx.Size(56, 26), style=0, value=u'0')

    def __init__(self, parent):
        self._init_ctrls(parent)

        self.myInit()
        
    def myInit(self):
        self.STATE_ADD = 0
        self.STATE_COPY = 1
        self.STATE_MODIFY = 2
        
        #默认为新增模式
        self.state = self.STATE_ADD
        self.curProduct = None
        
        #单位表格
        title_unit = [u'单位', u'关系', u'在手数量', u'预设成本', u'预设售价',
                        u'最近进价', u'最低进价', u'最高进价']
                        
        self.gridUnit.CreateGrid(3, len(title_unit))
        for i in range(len(title_unit)):
            self.gridUnit.SetColLabelValue(i, title_unit[i])
        self.gridUnit.SetSize(self.gridUnit.GetBestSize())  

        attr = wx.grid.GridCellAttr()
        attr.SetReadOnly(True)
        #attr.SetAlignment(wx.ALIGN_CENTRE, wx.ALIGN_CENTRE)
        self.gridUnit.SetColAttr(2, attr)  # 在数数量  只读

        self.TCID.SetValue(str(CDataProduct.newId()))
        
        self.curCategory = None
        self.curKeywords = []
        
    def getKeywordsStr(self):
        # str = ''
        # for obj in self.curKeywords:
            # str += '%s  ' %obj.getFieldVal(index=CDataKeyword.COL_NAME)
        # return str
        
        str = CDataProduct.get_sku_str(self.curProduct.getFieldVal(index=CDataProduct.COL_ID))
        print str
        return str if str is not None else ""
    
    def getProObj(self):
        return self.curProduct
    
    def setData(self, proObj, createNew=False):
        self.curProduct = proObj
        self.curProduct.refreshOnhand()
        
        proId = self.curProduct.getFieldVal(index=CDataProduct.COL_ID)
        if not (createNew):
            self.TCID.SetValue(str(proId))
            self.TCID.SetModified(False)
            self.state = self.STATE_MODIFY
        else:
            self.state = self.STATE_COPY
            self.btnStock.Enable(False)
            self.btnSearchKeyword.Enable(False)
                
        self.TCName.SetValue(self.curProduct.getFieldVal(index=CDataProduct.COL_NAME))
        self.TCSimCode.SetValue(self.curProduct.getFieldVal(index=CDataProduct.COL_BRIEFCODE))
        self.TCSpec.SetValue(self.curProduct.getFieldVal(index=CDataProduct.COL_SPEC))
        self.TCBrand.SetValue(self.curProduct.getFieldVal(index=CDataProduct.COL_BRAND))
        self.CheckOnUse.SetValue(self.curProduct.getFieldVal(index=CDataProduct.COL_ONUSE))
        self.chk_notcountable.SetValue(self.curProduct.getFieldVal(index=CDataProduct.COL_NOTCOUNTABLE))
        self.TCMemo.SetValue(self.curProduct.getFieldVal(index=CDataProduct.COL_MEMO))
        #self.TCPosition.SetValue(self.curProduct.getFieldVal(index=CDataProduct.COL_POSITION))
        self.TCLowestQty.SetValue(str(self.curProduct.getFieldVal(index=CDataProduct.COL_LOWEST_QTY)))
        self.checkBox_overstock.SetValue(self.curProduct.getFieldVal(index=CDataProduct.COL_OVERSTOCK))
        self.checkBox_promot.SetValue(self.curProduct.getFieldVal(index=CDataProduct.COL_PROMOT))

        #分类
        categoryId = self.curProduct.getFieldVal(index=CDataProduct.COL_CATEGORYID)
        if (categoryId is not None):
            self.curCategory = CDataCategory.getFirstInstance({CDataCategory.COL_ID:categoryId})
            self.TCType.SetValue(self.curCategory.getFieldVal(index=CDataCategory.COL_NAME))
            
        #单位信息
        for row in range(CDataProduct.UNIT_CNT):
            unitIndex = CDataProduct.COL_UNIT1 + row * CDataProduct.UNIT_FIELD_CNT
            unit = self.curProduct.getFieldVal(index=unitIndex)
            if (unit is None or unit == ""):
                break
            
            for col in range(CDataProduct.UNIT_FIELD_CNT):
                if (col == 2 and self.state == self.STATE_COPY):
                    # 复制新增时，数量设置为0
                    self.gridUnit.SetCellValue(row, col, "0")
                    continue

                val = self.curProduct.getFieldVal(index = unitIndex + col)
                if (val is not None):
                    self.gridUnit.SetCellValue(row, col, kit.myStr(val))
         
        #关键字
        #self.curKeywords = CDataProLabel.getKeywordObjList(proId)
        self.tcKeyword.SetValue(self.getKeywordsStr())
        
        # 新增价格
        self.tcMinProfit.SetValue(str(self.curProduct.getFieldVal(index=CDataProduct.COL_MINPROFIT)))
        self.tcLxProfit.SetValue(str(self.curProduct.getFieldVal(index=CDataProduct.COL_LX_PROFIT)))
        self.tcBulkProfit.SetValue(str(self.curProduct.getFieldVal(index=CDataProduct.COL_WHOLE_PROFIT)))
        self.tc_freight_charges.SetValue(str(self.curProduct.getFieldVal(index=CDataProduct.COL_FREIGHT)))
        self.tc_weight.SetValue(str(self.curProduct.getFieldVal(index=CDataProduct.COL_WEIGHT)))
        self.tcLength.SetValue(str(self.curProduct.getFieldVal(index=CDataProduct.COL_LENGTH)))
        self.tcArea.SetValue(str(self.curProduct.getFieldVal(index=CDataProduct.COL_AREA)))
        self.tcPackage.SetValue(str(self.curProduct.getFieldVal(index=CDataProduct.COL_PACKAGE)))
        
        self.tcLastCost.SetValue(str(self.curProduct.getFieldVal(index=CDataProduct.COL_LAST_COST)))
        self.tcCapacity.SetValue(str(self.curProduct.getFieldVal(index=CDataProduct.COL_CAPACITY)))
        self.tcImage.SetValue(self.curProduct.getFieldVal(index=CDataProduct.COL_IMGPREFIX))
                
    def saveProToDB(self):
        '''保存产品信息到数据库'''
        #Save the basic Info
        mPro = CDataProduct.factory(False) #从界面取得了id
        fields = {}
        
        proId = int(self.TCID.GetValue())
        name = self.TCName.GetValue()
        spec = self.TCSpec.GetValue()
        if (name == '' or spec == ''):
            if (name == '' and spec == ''):
                dlg = wx.MessageDialog(self,
                            u'没有填写商品名称和规格信息，二者必须至少填写一个',
                            u'提示', wx.OK|wx.ICON_ERROR)
                dlg.ShowModal()
                return False
            elif (name == ''):
                self.TCName.SetValue(spec)
                dlg = wx.MessageDialog(self, u'没有填写商品名称，自动补填商品规格',
                                        u'提示', wx.OK|wx.ICON_ERROR)
                dlg.ShowModal()
                return False
            elif (spec == ''):
                self.TCSpec.SetValue(name)
                dlg = wx.MessageDialog(self, u'没有填写商品规格，自动补填商品名称',
                                        u'提示', wx.OK|wx.ICON_ERROR)
                dlg.ShowModal()
                return False
        briefCode = self.TCSimCode.GetValue()
        
        if (self.curCategory is None):
            typeid = CDataCategory.getDefCategoryId()
        else:
            typeid = self.curCategory.getFieldVal(index=CDataCategory.COL_ID)
            
        brand = self.TCBrand.GetValue()
        onUse = int(self.CheckOnUse.GetValue())
        memo = self.TCMemo.GetValue()
        notcountable = int(self.chk_notcountable.GetValue())
        
        fields[CDataProduct.COL_ID] = proId
        fields[CDataProduct.COL_NAME] = name
        fields[CDataProduct.COL_SPEC] = spec
        fields[CDataProduct.COL_BRIEFCODE] = briefCode
        fields[CDataProduct.COL_CATEGORYID] = typeid
        fields[CDataProduct.COL_BRAND] = brand
        fields[CDataProduct.COL_ONUSE] = onUse
        fields[CDataProduct.COL_OVERSTOCK] = int(self.checkBox_overstock.GetValue())
        fields[CDataProduct.COL_PROMOT] = int(self.checkBox_promot.GetValue())
        fields[CDataProduct.COL_MEMO] = memo
        #fields[CDataProduct.COL_POSITION] = self.TCPosition.GetValue()
        fields[CDataProduct.COL_NOTCOUNTABLE] = notcountable
        fields[CDataProduct.COL_LOWEST_QTY] = float(self.TCLowestQty.GetValue())
        fields[CDataProduct.COL_MINPROFIT] = float(self.tcMinProfit.GetValue())
        fields[CDataProduct.COL_LX_PROFIT] = float(self.tcLxProfit.GetValue())
        fields[CDataProduct.COL_WHOLE_PROFIT] = float(self.tcBulkProfit.GetValue())
        fields[CDataProduct.COL_FREIGHT] = float(self.tc_freight_charges.GetValue())
        fields[CDataProduct.COL_WEIGHT] = float(self.tc_weight.GetValue())
        fields[CDataProduct.COL_LENGTH] = float(self.tcLength.GetValue())
        fields[CDataProduct.COL_AREA] = float(self.tcArea.GetValue())
        fields[CDataProduct.COL_PACKAGE] = int(self.tcPackage.GetValue())
        fields[CDataProduct.COL_LAST_COST] = float(self.tcLastCost.GetValue() if self.tcLastCost.GetValue()!="" else 0)
        fields[CDataProduct.COL_CAPACITY] = float(self.tcCapacity.GetValue() if self.tcCapacity.GetValue()!="" else 0)
        fields[CDataProduct.COL_IMGPREFIX] = self.tcImage.GetValue()
        mPro.setMulFieldVal(fields)    

        #设置单位信息
        unitVal = []
        bBlank = False
        for i in range(self.gridUnit.GetNumberRows()):              
            for j in range(self.gridUnit.GetNumberCols()):
                val = self.gridUnit.GetCellValue(i, j)
                if (val == ''):
                    if(j == 0):# None Unit
                        bBlank = True
                    else:
                        val = None
                if (bBlank and val != '' and val is not None):
                    log(DBG, 'i=%d j=%d val=%s', (i,j,val))
                    msg = u'单位设置：第%d行缺失单位名称，请检查输入' %(i+1)
                    dlg = wx.MessageDialog(self, msg,
                                        u'提示', wx.OK|wx.ICON_ERROR)
                    dlg.ShowModal()
                    return False
                
                if (not bBlank and j == 2 and (val == '' or val is None)):
                    val = '0' # by lv 200926 - onhand为none会导致启动时异常退出
                unitVal.append(val)
                
        if (unitVal[0] == ''):
            dlg = wx.MessageDialog(self, u'单位设置：第1行缺失单位名称，请检查输入',
                                        u'提示', wx.OK|wx.ICON_ERROR)
            dlg.ShowModal()
            return False
        
        mPro.setUnitData(unitVal)

        b_no_sku = False
        if(self.state == self.STATE_MODIFY and self.curProduct is not None):
            mPro.update()
        else:
            mPro.insert()
            b_no_sku = True
        self.curProduct = mPro
        #mPro.updateQuantity(mPro.getFieldVal(index=mPro.COL_ONHAND1))

        if (b_no_sku):
            self.OnBtnSearchKeywordButton(None)
        #self.refreshViewProductList()
        
        #Set Keyword
        # CDataProLabel.delData({CDataProLabel.COL_PROID:proId})
        # for i in range(len(self.curKeywords)):
            # obj = CDataProLabel.factory()
            # fields = {CDataProLabel.COL_PROID:proId,
                        # CDataProLabel.COL_LABELID:self.curKeywords[i].getFieldVal(index=CDataKeyword.COL_ID)}
            # obj.setMulFieldVal(fields)
            # obj.insert()
        
        self.curProduct = mPro
        return True
           
    def OnBtnSearchCategoryButton(self, event):
        dlg = DlgCategory.create(self)
        if (self.curCategory is not None):
            dlg.setSelection(self.curCategory)
        btn = event.GetEventObject()
        btnPos = btn.ClientToScreen( (0,0) )
        btnSize =  btn.GetSize()
        dlgSize = dlg.GetSize()
        dlg.Move((btnPos.x-dlgSize[0]/2, btnPos.y+btnSize[1]))
        if (dlg.ShowModal() == wx.ID_CANCEL):
            return
        
        self.curCategory = dlg.categorySel
        if (self.curCategory is None):
            self.TCType.SetValue('')
        else:
            self.TCType.SetValue(self.curCategory.getFieldVal(index=CDataCategory.COL_NAME))

    def OnBtnSearchKeywordButton(self, event):
        # dlg = DlgKeyword.create(self)
        # btn = event.GetEventObject()
        # btnPos = btn.ClientToScreen( (0,0) )
        # btnSize =  btn.GetSize()
        # dlgSize = dlg.GetSize()
        # dlg.Move((btnPos.x-dlgSize[0]/2, btnPos.y+btnSize[1]))
        # if (dlg.ShowModal() == wx.ID_CANCEL):
            # return

        # self.curKeywords = dlg.keywordSel
        # self.tcKeyword.SetValue(self.getKeywordsStr())

        # if (self.state == self.STATE_COPY or self.state == self.STATE_ADD):
        #     dlg = wx.MessageDialog(self, u'新建商品，保存之后才能编辑',
        #                                 u'提示', wx.OK|wx.ICON_WARNING)
        #     dlg.ShowModal()
        #     return

        import DlgSetProductSku
        
        dlg = DlgSetProductSku.create(self
            , self.curProduct.getFieldVal(index=CDataProduct.COL_ID))
        if (dlg.ShowModal() == wx.ID_CANCEL):
            return
        self.tcKeyword.SetValue(self.getKeywordsStr())    
            
    def OnBtnSaveButton(self, event):
        if(self.saveProToDB()):
            #产品信息发生变化，刷新缓存的信息
            #关系型数据表不用刷新，因为数据取用都是通过即时从数据库中获取
            CDataProduct.refreshDataList()
            self.EndModal(wx.ID_OK)

    def OnBtnCancelButton(self, event):
        self.EndModal(wx.ID_CANCEL)

    def OnTCNameText(self, event):
        name = self.TCName.GetValue()
        code = CBriefCode.getBriefCode(name)
        self.TCSimCode.SetValue(code)

    def OnGridUnitGridCellChange(self, event):
        '''设置单位名称时，自动把价格设置为0'''
        print 'OnGridUnitGridCellChange'
        if (event.GetCol() != 0):
            return
        
        row = event.GetRow()
        for col in range(2,8):     
            if (self.gridUnit.GetCellValue(row, col) == ''):
                self.gridUnit.SetCellValue(row, col, '0.0')

    def OnBtnSwitchButton(self, event):
        import DlgQuickAddProduct
        self.EndModal(wx.ID_CANCEL)
        
        dlg = DlgQuickAddProduct.create(self.GetParent())
        dlg.ShowModal()

    def OnTcMinProfitText(self, event):
        event.Skip()

    def OnTcLxProfitText(self, event):
        event.Skip()

    def OnTcBulkProfitText(self, event):
        event.Skip()

    def OnBtnStockButton(self, event):
        import DlgProductStock

        if(self.state == self.STATE_COPY):
            return

        dlg = DlgProductStock.create(self)
        dlg.SetProduct(self.curProduct.getFieldVal(index=CDataProduct.COL_ID))
        dlg.ShowModal()
        
        
        
